# -*- coding: utf-8 -*-
# @Time    : 2020/3/14 下午6:30
# @Author  : lilong

from abc import ABCMeta, abstractmethod


# ==============================
# 代码框架
# ==============================
class Flyweight(metaclass=ABCMeta):
    """享元类：抽象基类"""

    @abstractmethod
    def operation(self, extrinsicState):
        pass


class FlyweightImpl(Flyweight):
    """享元类的具体实现类"""

    def __init__(self, color):
        self.__color = color

    def operation(self, extrinsicState):
        print("%s 取得 %s色颜料" % (extrinsicState, self.__color))


class FlyweightFactory:
    """享元工厂"""

    def __init__(self):
        self.__flyweights = {}   # 感觉有问题

    def getFlyweight(self, key):
        pigment = self.__flyweights.get(key)
        if pigment is None:
            pigment = FlyweightImpl(key)
        return pigment


# 基于框架的实现
# ==================================
def testFlyweight():

    # 工厂类
    factory = FlyweightFactory()

    #
    pigmentRed = factory.getFlyweight("红")
    pigmentRed.operation("梦之队")
    pigmentYellow = factory.getFlyweight("黄")
    pigmentYellow.operation("梦之队")
    pigmentBlue1 = factory.getFlyweight("蓝")
    pigmentBlue1.operation("梦之队")
    pigmentBlue2 = factory.getFlyweight("蓝")
    pigmentBlue2.operation("和平队")


testFlyweight()
